<?php

//require_once "Mail.php";
//require_once('class.phpmailer.php');

if (!defined('DATABASE_NAME')) {
    define("DATABASE_NAME", "default");
}

class zone_model extends CI_Model {

    var $id;
    var $name;
    var $file;
    var $DBControle;

    function __construct($id = '', $name = '', $file = '') {
        parent::__construct();

        $this->id = $id;
        $this->name = $name;
        $this->file = $file;
    }

    function id($id) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->where('z0_id', $id);
        $query = $this->DBControle->get('Zone');
        $result = $query->result();

        if (count($result) > 0) {
            $zone = $result[0];

            $this->id = $zone->z0_id;
            $this->name = $zone->z0_name;
            $this->file = $zone->z0_file;

            return $this;
        } else {
            return false;
        }
    }

    function adicionar($name, $file, $ip) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $insert["z0_name"] = $name;
        $insert["z0_file"] = $file;

        if ($name != '' && $file != '') {
            $this->DBControle->insert('Zone', $insert);
            return $this->DBControle->insert_id();
        } else {
            return false;
        }
    }

    function editar($id, $name = false, $file = false) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $update = array();

        if ($name) {
            $update["z0_name"] = $name;
        }
        if ($file) {
            $update["z0_file"] = $file;
        }

        if (count($update) > 0) {
            $this->DBControle->where('z0_id', $id);
            $this->DBControle->update('Zone', $update);
        }
    }

    function remover() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
    }

    function listar() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('Zone');
        $result = $query3->result();
        $lista = array();
        foreach ($result as $key => $value) {
            $lista[] = new Zone_model($value->z0_id, $value->z0_name, $value->z0_file);
        }

        return $lista;
    }

    function hosts($id) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $this->DBControle->where('z0_user', $id);
        $query3 = $this->DBControle->get('Zone');
        $result = $query3->result();
        $lista = array();
        foreach ($result as $key => $value) {
            $lista[] = new Zone_model($value->z0_id, $value->z0_name, $value->z0_file);
        }

        return $lista;
    }

    function pagina($numerpdapagina, $resultadosporpagina = 15) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('Zone');
        $result = $query3->result();
        $lista = array();
        foreach ($result as $key => $value) {
            $lista[] = new Zone_model($value->z0_id, $value->z0_name, $value->z0_file);
        }

        $listapagina = array_slice($lista, $numerpdapagina, $resultadosporpagina);

        $retorno = array();
        $retorno[0] = count($lista);
        $retorno[1] = $listapagina;

        return $retorno;
    }

}

?>
